package portal.manager.data;

import java.util.UUID;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
 * 
 * @author Sorrentino
 *
 */
public class ServiceBean {

	/**
	 * 
	 * @param context
	 * @param ref
	 * @return
	 */
	public static ServiceBean createFromReference( BundleContext context, ServiceReference ref ) {
	
		ServiceBean result = new ServiceBean();
		
		StringBuilder builder = new StringBuilder(100);
		for( String key : ref.getPropertyKeys() ) {
			
			if( "objectClass".equals( key ) ) {
				String [] classes = (String[])ref.getProperty(key);
				
				result.name = classes[0];
				
			}
			else {
				builder.append( key ).append( '=' ).append( ref.getProperty(key)).append(';');
			}
		}
		
		result.id = UUID.randomUUID().toString();
		result.attributes = builder.toString();
		
		return result;
		
	}
	
	String attributes;
	Object action;
	String id;
	String name;
	
	/**
	 * 
	 */
	private ServiceBean() {
		action = "function( context ) {}";
	}

	
	public final String getName() {
		return name;
	}


	public final String getId() {
		return attributes;
	}

	public final String getAttributes() {
		return attributes;
	}
	
	public final Object getAction() {
		return action;
	}

	
}
